<?php
require_once 'conn.php';
class Tool {
	//根据成员ID更新组状态（删除，修改）
	public function updateGroupStateByMemberId($id) {
		$sql = "select groupId from member where id=" . $id;
		$result = mysql_query ( $sql );
		$row = mysql_fetch_array ( $result );
		$groupId = $row ["groupId"];
		$res = $this->updateGroupStateByGroupId ( $groupId );
		return $res;
	}
	//根据组ID更新组状态（增加）
	public function updateGroupStateByGroupId($groupId) {
		$sql2 = "select id,status from member where groupId=" . $groupId;
		$res = mysql_query ( $sql2 );
		$sum = 0; //状态和
		$count = 0; // 人数
		while ( $r = mysql_fetch_array ( $res ) ) {
			if ($r ["status"] == "正常") {
				$sum += 2;
			} else if ($r ["status"] == "忙碌") {
				$sum += 3;
			} else {
				$sum += 1;
			}
			$count ++;
		}
		$res = $sum / $count;
		if ($res < 1.7)
			$status = "空闲";
		else if ($res >= 1.7 && $res < 2.3)
			$status = "正常";
		else
			$status = "忙碌";
		$sql3 = "update team set status='" . $status . "' where id=" . $groupId;
		$result = mysql_query ( $sql3 );
		return $result;
	}
}
?>